﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace ControlExplorer.Controls.LayoutPanels
{
    public partial class DemoWrapPanel
        : UserControl, IExposeApplicationBarItems
    {
        ApplicationBarIconButton _btnAddItem = new ApplicationBarIconButton();

        public DemoWrapPanel()
        {
            InitializeComponent();

            _btnAddItem.Text = "add item";
            _btnAddItem.IconUri = new Uri("/Resources/AppBar/plus.png", UriKind.Relative);
            _btnAddItem.Click += _btnAddItem_Click;

            panel.Children.Clear();
            for (int i = 0; i < 9; i++)
            {
                AddItem();
            }
        }

        int[] _widths = { 140, 140, 160, 200, 140, 140, 100 };

        string[] _colors = { "#FF008B9C", "#FF0094D6", "#FF497331", "#FF9DCFC3", "#FF005B84" };
        Random _rnd = new Random();
        private void AddItem()
        {
            panel.Children.Add(new ContentControl() 
            {
                Content = (panel.Children.Count + 1).ToString(),
                Width = _rnd.PickOne<int>(_widths),
                Background = new SolidColorBrush(_rnd.PickOne<string>(_colors).ToColor()),
                Template = (ControlTemplate)Resources["PanelItemTemplate"]
            });
        }

        void _btnAddItem_Click(object sender, EventArgs e)
        {
            AddItem();
        }

        public IEnumerable<IApplicationBarMenuItem> ApplicationBarItems
        {
            get 
            {
                yield return _btnAddItem;
            }
        }
    }

}